﻿using System;
using System.Collections.Generic;

namespace RaisingStudio.SmallProgram.Library
{
    [SmallProgramType]
    public static class Array
    {
        private static Dictionary<Primitive, Dictionary<Primitive, Primitive>> _arrayMap = new Dictionary<Primitive, Dictionary<Primitive, Primitive>>();

        public static Primitive IsArray(Primitive array)
        {
            return array.IsArray;
        }

        internal static Dictionary<Primitive, Primitive> GetArray(Primitive arrayName)
        {
            Dictionary<Primitive, Primitive> dictionary;
            if (_arrayMap.TryGetValue(arrayName, out dictionary))
            {
                return dictionary;
            }
            return null;
        }

        public static Primitive ContainsIndex(Primitive array, Primitive index)
        {
            return array.ContainsKey(index);
        }

        public static Primitive ContainsValue(Primitive array, Primitive value)
        {
            return array.ContainsValue(value);
        }

        public static Primitive GetAllIndices(Primitive array)
        {
            return array.GetAllIndices();
        }

        public static Primitive GetItemCount(Primitive array)
        {
            return array.GetItemCount();
        }


        [HideFromIntellisense]
        public static Primitive GetValue(Primitive arrayName, Primitive index)
        {
            Primitive primitive;
            Dictionary<Primitive, Primitive> array = GetArray(arrayName);
            if ((array != null) && array.TryGetValue(index, out primitive))
            {
                return primitive;
            }
            return "";
        }

        [HideFromIntellisense]
        public static void SetValue(Primitive arrayName, Primitive index, Primitive value)
        {
            if (!arrayName.IsEmpty)
            {
                Dictionary<Primitive, Primitive> dictionary;
                if (!_arrayMap.TryGetValue(arrayName, out dictionary))
                {
                    dictionary = new Dictionary<Primitive, Primitive>();
                    _arrayMap[arrayName] = dictionary;
                }
                dictionary[index] = value;
            }
        }

        [HideFromIntellisense]
        public static void RemoveValue(Primitive arrayName, Primitive index)
        {
            Dictionary<Primitive, Primitive> array = GetArray(arrayName);
            if (array != null)
            {
                array.Remove(index);
            }
        }
    }
}

